package edu.java.homework.hw09.exercises.person;

public class Person implements Comparable<Person> {

    // --- Instance Variables --------------------------------------------------
    private String fisrtName = null;
    private String secondName = null;
    private String thirdName = null;
    private int age = 0;
    private Gender gender = Gender.MALE;
    private String city = null;
    private int kids = 0;

    // --- Constructors --------------------------------------------------------
    public Person() {
	this(null, null, null, 0, Gender.MALE, null, 0);
    }

    public Person(String firstName, String secondName, String thirdName,
	    int age, Gender gender, String city, int kids) {
	this.fisrtName = firstName;
	this.secondName = secondName;
	this.thirdName = thirdName;
	this.age = age;
	this.gender = gender;
	this.city = city;
	this.kids = kids;

    }

    // --- Methods -------------------------------------------------------------
    @Override
    public boolean equals(Object obj) {
	if (!(obj instanceof Person)) {
	    throw new IllegalArgumentException(
		    "Incorrect type of the input object.");
	}
	Person person = (Person) obj;

	return (this.getFisrtName().equals(person.getFisrtName()))
		&& (this.getSecondName().equals(person.getSecondName()))
		&& (this.getThirdName().equals(person.getThirdName()))
		&& (this.getGender().equals(person.getGender()))
		&& (this.getCity().equals(person.getCity()))
		&& (this.getAge() == person.getAge())
		&& (this.getKids() == person.getKids());

    }

    @Override
    public String toString() {
	// [име презиме фамилия, възраст г, пол, град-на-местоживеене, брой-деца
	// дете/деца]

	String BLANK_SPACE = " ";
	String COMMA = ", ";
	StringBuilder sb = new StringBuilder();
	sb.append("[ ");
	sb.append(this.getFisrtName());
	sb.append(BLANK_SPACE);
	sb.append(this.getSecondName());
	sb.append(BLANK_SPACE);
	sb.append(this.getThirdName());
	sb.append(COMMA);
	sb.append(this.getAge());
	sb.append(BLANK_SPACE);
	sb.append("year(s)");
	sb.append(COMMA);
	sb.append(getGender() != null ? getGender().toString().toLowerCase()
		: null);
	sb.append(COMMA);
	sb.append(this.getCity());
	sb.append(COMMA);
	sb.append(this.getKids());
	sb.append("kid(s) ]");

	return sb.toString();

    }

    @Override
    public int compareTo(Person person) {

	// check input parameters
	if (person == null) {
	    System.out.println(this.toString());
	    throw new IllegalArgumentException("Person object is null pointed.");
	}
	// System.out.println(person.toString());

	// check first, second and third name

	int compareFirstName = this.getFisrtName().compareTo(
		person.getFisrtName());
	if (compareFirstName != 0) {
	    // System.out.print("First Name Diference: ");
	    return compareFirstName;
	}

	int compareSecondName = this.getSecondName().compareTo(
		person.getSecondName());
	if (compareSecondName != 0) {
	    // System.out.print("Second Name Diference: ");
	    return compareSecondName;
	}

	int compareThirdName = this.getThirdName().compareTo(
		person.getThirdName());
	if (compareThirdName != 0) {
	    // System.out.print("Third Name Diference: ");
	    return compareThirdName;
	}

	// check age
	int compareAge = this.getAge() - person.getAge();
	if (compareAge != 0) {
	    // System.out.print("Age Diference: ");
	    return compareAge;
	}

	// check Gender
	int compareGender = this.getGender().compareTo(person.getGender());
	if (compareGender != 0) {
	    // System.out.print("Gender Diference: ");
	    return compareGender;
	}

	// check city
	int compareCity = this.getCity().compareTo(person.getCity());
	if (compareCity != 0) {
	    // System.out.print("City Diference: ");
	    return compareCity;
	}

	// check kids
	int compareKids = this.getKids() - person.getKids();
	// if (compareKids != 0) {
	// // System.out.print("Kids Diference: ");
	// } else {
	// System.out.print("Equal : ");
	// }

	return compareKids;
    }

    // --- Getters and Setters -------------------------------------------------

    public String getFisrtName() {
	return fisrtName;
    }

    public void setFisrtName(String fisrtName) {
	// check input parameters
	if (fisrtName == null) {
	    throw new IllegalArgumentException(
		    "FirstName is null-pointed string");
	}
	this.fisrtName = fisrtName;
    }

    public String getSecondName() {
	return secondName;
    }

    public void setSecondName(String secondName) {
	// check input parameters
	if (secondName == null) {
	    throw new IllegalArgumentException(
		    "SecondName is null-pointed string");
	}
	this.secondName = secondName;
    }

    public String getThirdName() {
	return thirdName;
    }

    public void setThirdName(String thirdName) {
	// check input parameters
	if (thirdName == null) {
	    throw new IllegalArgumentException(
		    "ThirdName is null-pointed string");
	}
	this.thirdName = thirdName;
    }

    public int getAge() {
	return age;
    }

    public void setAge(int age) {
	// check input parameters
	if (age < 0) {
	    throw new IllegalArgumentException(
		    "Age is negative. It should be positive number");
	}
	this.age = age;
    }

    public Gender getGender() {
	return gender;
    }

    public void setGender(Gender gender) {
	// check input parameters
	if (gender == null) {
	    throw new IllegalArgumentException("Gender is null-pointed object."
		    + "Gender can have only male and femay as value.");
	}
	this.gender = gender;
    }

    public String getCity() {
	return city;
    }

    public void setCity(String city) {
	// check input parameters
	if (city == null) {
	    throw new IllegalArgumentException("City is null-pointed string.");
	}
	this.city = city;
    }

    public int getKids() {
	return kids;
    }

    public void setKids(int kids) {
	// check input parameters
	if (kids < 0) {
	    throw new IllegalArgumentException(
		    "Age is negative. It should be positive number");
	}
	this.kids = kids;
    }

}
